package com.yssoft.service.equipment;

import com.yssoft.entity.core.TCode;
import com.yssoft.entity.equipment.TEquipmentImgP;
import com.yssoft.entity.equipment.TEquipmentImgT;
import com.yssoft.entity.equipment.TEquipmentP;
import com.yssoft.entity.equipment.TEquipmentT;
import com.yssoft.entity.inventory.TWarehouse;
import com.yssoft.mapper.core.TCodeMapper;
import com.yssoft.mapper.core.TDdDetailMapper;
import com.yssoft.mapper.core.TPersonnelMapper;
import com.yssoft.mapper.equipment.TEquipmentImgTMapper;
import com.yssoft.mapper.equipment.TEquipmentPMapper;
import com.yssoft.mapper.equipment.TEquipmentTMapper;
import com.yssoft.mapper.inventory.TWarehouseMapper;
import com.yssoft.utils.AppUserHelper;
import com.yssoft.utils.AppUtil;
import com.yssoft.utils.UploadFile;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @类名称:  TEquipmentTService
 * @类描述: 检验设备service
 * @作者: 于兴达
 * @创建时间:2018年9月7日 下午1:31:58
 */
@Service
public class EquipmentTService {

	@Resource
    private TEquipmentTMapper equipmentMapper;
    @Resource
    private TEquipmentImgTMapper equipmentImgTMapperMapper;
    /**
     * 获取生产设备列表
     * @author 于兴达
     * @param companyId
     * @param name
     * @return
     */
	public List<Map<String, Object>> findAll(int page,int limit,Integer companyId,String name) {
		HashMap<String,Object> param = new HashMap<>();
		param.put("companyId",companyId);
		param.put("name",name);
		if(limit>0){
			int start = ((page - 1) * limit);
			int end = limit;
			param.put("start", start);
			param.put("end", end);
		}else{
			param.put("start", null);
			param.put("end", null);
		}
		List<Map<String, Object>> list = equipmentMapper.selectEquipmentT(param);
//		for (Map<String, Object> map : list) {
//		List<Map<String,Object>> personList=equipmentMapper.queryPersonByWarehouseId(Integer.valueOf(map.get("id").toString()));
//		map.put("personList", personList);
//		}
		return  list;
	}
	/**
     * @方法名称: getOne
     * @功能描述: 根据检验设备id查询检验设备
     * @作者: 于兴达
     * @创建时间:2018年9月7日 下午19:14:38
     * @param id 生产设备id
     * @return Object
     */
	public Map<String, Object> getOne(String id) {
		HashMap<String, Object> equipmentt = new HashMap<String ,Object>();
		equipmentt.put("equipmentp", equipmentMapper.selectById(Integer.valueOf(id)));
		equipmentt.put("img", equipmentMapper.queryImageByequipmentpId(Integer.valueOf(id)));
		return equipmentt;
	}
	/**
     * @方法名称: saveEquipmentT
     * @功能描述: 保存检验设备
     * @作者: 于兴达
     * @创建时间:2018年9月7日 下午19:18:38
     * @param equipmentT 检验设备实体
	 * @param turls 
	 * @param urls 
     * @return Object
     */
	@Transactional
	public Object saveEquipmentT(TEquipmentT equipmentT, MultipartFile[] files, HttpServletRequest request) {
		Integer wId;
		if(equipmentT.getId()!=null){
			wId=equipmentT.getId();
			equipmentMapper.updateByPrimaryKeySelective(equipmentT);
			TEquipmentImgT equipmentImgT1 = new TEquipmentImgT();
			equipmentImgT1.settEquipmentTId(wId);
			List<TEquipmentImgT> equipmentImgTList = equipmentImgTMapperMapper.select(equipmentImgT1);
			for (TEquipmentImgT tEquipmentImgT : equipmentImgTList) {
				equipmentImgTMapperMapper.delete(tEquipmentImgT);
				equipmentImgTMapperMapper.updateByPrimaryKeySelective(tEquipmentImgT);
			}
			MultipartRequest mRequest=(MultipartRequest)request;
			Iterator<String> fileNames = mRequest.getFileNames();
			while(fileNames.hasNext()){
				MultipartFile file = mRequest.getFile(fileNames.next());
				Map<String,Object> urlMap = null;
                try {
                    urlMap = UploadFile.compressUploadedFile(file);
                } catch (Exception e) {
                    return AppUtil.returnMapJson("0", "上传图片失败" + e.getMessage(), null);
                }
                TEquipmentImgT equipmentImgT = new TEquipmentImgT();
                equipmentImgT.setCreateTime(new Date());
                equipmentImgT.settEquipmentTId(wId);
                equipmentImgT.setTurl(urlMap.get("thumbnails")+"");
                equipmentImgT.setUrl(urlMap.get("fileName")+"");
				equipmentImgTMapperMapper.insertSelective(equipmentImgT);
			}
			return AppUtil.returnMapJson("1", "保存成功!", null);
		}else{
			equipmentT.setCreateTime(new Date());
			equipmentT.setCreatorId(String.valueOf(AppUserHelper.getCurrentUserId(request)));
			equipmentT.setCreatorName(AppUserHelper.getCurrentUserName(request));
			equipmentT.setDeptName(AppUserHelper.getCurrentDeptName(request));
			equipmentT.setDeptId(AppUserHelper.getCurrentDeptId(request));
			equipmentT.settEId(AppUserHelper.getCurrentCompanyId(request));
			equipmentT.setDeleteFlag("0");
			equipmentMapper.insertSelective(equipmentT);
			 wId = equipmentT.getId();
			 MultipartRequest mRequest=(MultipartRequest)request;
				Iterator<String> fileNames = mRequest.getFileNames();
				while(fileNames.hasNext()){
					MultipartFile file = mRequest.getFile(fileNames.next());
					Map<String,Object> urlMap = null;
	                try {
	                    urlMap = UploadFile.compressUploadedFile(file);
	                } catch (Exception e) {
	                    return AppUtil.returnMapJson("0", "上传图片失败" + e.getMessage(), null);
	                }
	                TEquipmentImgT equipmentImgT = new TEquipmentImgT();
	                equipmentImgT.setCreateTime(new Date());
	                equipmentImgT.settEquipmentTId(wId);
	                equipmentImgT.setTurl(urlMap.get("thumbnails")+"");
	                equipmentImgT.setUrl(urlMap.get("fileName")+"");
					 equipmentImgTMapperMapper.insertSelective(equipmentImgT);
				}
				return AppUtil.returnMapJson("1", "保存成功!", null);
		}
	}
	 /**
     * @方法名称: deleteEquipmentP
     * @功能描述: 删除检验设备
     * @作者: 于兴达
     * @创建时间:2018年9月7日 下午19:52:38
     * @param id 设备id
     * @return Object
     */
	public void deleteEquipmentT(String id) {
		equipmentMapper.deleteEquipmentT(Integer.valueOf(id));
		
	}
	/**
     * @方法名称: deleteImg
     * @功能描述: 删除图片
     * @作者: 于兴达
     * @创建时间:2018年9月08日 上午11:35:38
     * @return Map<String, Object>
     */
	public void deleteImg(String id) {
		equipmentImgTMapperMapper.deleteByPrimaryKey(id);
		
	}
}


